//
//  MineViewController.m
//  Runone
//
//  Created by runone01 on 16/9/6.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "MineViewController.h"
#import "MessafePushViewController.h"
#import "LoadMapViewController.h"
#import "ChangePassViewController.h"
#import "WorkRegistrationViewController.h"
#import "FeedbackViewController.h"
#import "RequestHelper.h"
#import "MJExtension.h"
#import "SysPersonalSettingInfoModel.h"
#import "LoginViewController.h"
#import "UIView+Message.h"
#import "PromptUtil.h"
#import "AppLocalManager.h"
#import "BCNetAPIClient.h"
//#import "UIColor+ZXLazy.h"
#import "SysUserInfo.h"
#import "OrganizationInfoModel.h"
#import "AppLocalManager.h"
#import "OfflineDetailViewController.h"
#import "AppDelegate.h"

@interface MineViewController ()<UITableViewDelegate, UITableViewDataSource,UIPopoverPresentationControllerDelegate,UIGestureRecognizerDelegate,UIAlertViewDelegate>
{
    OfflineDetailViewController *_detailViewController;
}

@property (weak, nonatomic) IBOutlet UIImageView *logoImageView;// logo

@property (weak, nonatomic) IBOutlet UITableView *InfoTableView;
@property(nonatomic,strong)NSArray *dataSource;
@property(nonatomic,strong)MessafePushViewController *pushVC;
@property(nonatomic,strong)FeedbackViewController *FaceBookVc;
@property(nonatomic,strong)LoadMapViewController *loadMapVc;
@property(nonatomic,strong)ChangePassViewController *passVC;
@property(nonatomic,strong)WorkRegistrationViewController *workReg;
@property(nonatomic,strong)SysUserInfo *UserBasemodel;
@property(nonatomic,strong)LoginViewController *loginVC;
@property (weak, nonatomic) IBOutlet UILabel *partment;
@property (weak, nonatomic) IBOutlet UILabel *Name;
@property (nonatomic) CFTimeInterval minimumPressDuration;
@property (nonatomic) CGFloat allowableMovement;
@property(nonatomic,copy)NSString *changState;
@property(nonatomic,strong)UIView *Tview;
@property (weak, nonatomic) IBOutlet UIView *UserLogin;
@property(nonatomic,strong)OrganizationInfoModel *OrganizationModel;
@end

@implementation MineViewController

#pragma    -------我的主界面

- (void)viewDidLoad {
    [super viewDidLoad];

    _loginVC=[[LoginViewController alloc] init];
    self.InfoTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 30)];
    label.text  = @"个人设置";
    label.textAlignment=NSTextAlignmentCenter;
    label.textColor=[UIColor whiteColor];
    self.navigationItem.titleView=label;
    [self initViewController];
    [self requestUserBase];
    [self changeUerImage];//更改用户图片
    
    _Name.textAlignment  = NSTextAlignmentLeft;
}
//点击我的
-(void)changeUerImage
{
    UILongPressGestureRecognizer *change=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(change:)];
    
    change.delegate=self;
    change.minimumPressDuration = 0.5;
    // "容错的范围"
    change.allowableMovement  = 10;
    [_UserLogin addGestureRecognizer:change];
}

-(void)change:(UILongPressGestureRecognizer *)longGesture
{
    if (longGesture.state==UIGestureRecognizerStateBegan) {
        NSLog(@"dd");
    }
   
}
- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated]           ;
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = YES ;
}
//初始化个人设置界面
-(void)initViewController {
    _UserBasemodel=[[SysUserInfo alloc] init];
    _detailViewController = [[OfflineDetailViewController alloc] init];
//    _detailViewController.mapView = self.mapView;
    //    detailViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
   
    _passVC=[[ChangePassViewController alloc] init];
    __weak typeof(self) weakSelf=self;
    _passVC.changePassBlock=^(NSString *str){
        _changState=str;
        if ([weakSelf.changState isEqualToString:@"1"]) {
//            _Tview=MessageViewFrame;
//            [weakSelf.Tview prompt:@"修改成功"];
//            [weakSelf.view addSubview:weakSelf.Tview];
            [weakSelf presentViewController:weakSelf.loginVC animated:YES completion:nil];
        }
        else
        {
            _Tview=MessageViewFrame;
            [weakSelf.Tview prompt:@"修改失败！~"];
            [weakSelf.view addSubview:weakSelf.Tview];
        }

    };
       _pushVC=[[MessafePushViewController alloc] init];
    _FaceBookVc = [[FeedbackViewController alloc] init];
    _workReg=[[WorkRegistrationViewController alloc] init];
    _passVC.modalPresentationStyle=UIModalPresentationPopover;
    
    _workReg.modalPresentationStyle=UIModalPresentationPopover;
    
    self.navigationController.navigationBar.hidden = NO;
    _dataSource=[[NSArray alloc] init];
    
    NSString *str = GetSys_RightCode;
    if ([str containsString:@"P010201"]) {
        _dataSource=@[@"值班登记",@"消息推送",@"意见反馈",@"离线地图",@"修改密码",@"退出"];
    }else {
    _dataSource=@[@"消息推送",@"意见反馈",@"离线地图",@"修改密码",@"退出"];
    }

    
    [_InfoTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellID"];
    _InfoTableView.scrollEnabled=NO;
    _InfoTableView.separatorColor = [UIColor grayColor];
    _InfoTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    _InfoTableView.delegate  = self;
    _InfoTableView.dataSource=self;
}


-(void)dis {
    [self presentViewController:_loginVC animated:YES completion:nil];
}

#pragma mark --新版接口
-(void)requestUserBase {
    RequestHelper *requestHelper=[[RequestHelper alloc] init];
    RequestGinsengModel *getCurrentEventModel=[[RequestGinsengModel alloc] init];
    getCurrentEventModel.URL=GetCurrentDutyPersonalName;
    getCurrentEventModel.DataValue = @"";
    getCurrentEventModel.UpdatedTime=@"";
    getCurrentEventModel.SystemCode=@"GuangleHighway";
    getCurrentEventModel.MethodName=@"GetUserInfo";
    
    requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel){
        
        NSDictionary *dic=[requestOutginseng.DataValue mj_JSONObject];
        _UserBasemodel=[SysUserInfo mj_objectWithKeyValues:dic];
        if (_UserBasemodel.UserName) {
            _partment.text=[NSString stringWithFormat:@"%@",_UserBasemodel.UserName];
        }
        NSArray *arr=_UserBasemodel.OrganizationInfoList;
        if (arr.count>0) {
            NSDictionary *dic=arr[0];
                _OrganizationModel=[OrganizationInfoModel mj_objectWithKeyValues:dic];
            if (_OrganizationModel.OrgName) {
                _Name.text=[NSString stringWithFormat:@"%@",_OrganizationModel.OrgName];
              //  _Name.textAlignment=NSTextAlignmentCenter;
            }
            else
            {
                NSLog(@"暂时没值");
                return ;
            }
        }
        else
        {

            _Name.text=@"暂无部门";
           // _Name.textAlignment=NSTextAlignmentCenter;
            NSLog(@"数组无数据");
            return ;
        }
        
        NSLog(@"%@",_Name.text);
//        _pushVC.model=_UserBasemodel;
    };
    
    [requestHelper RequestGinsengModel:getCurrentEventModel andPushController:self];
}


#pragma  mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath];
    
    cell.textLabel.text = self.dataSource[indexPath.row];
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.highlightedTextColor = [UIColor whiteColor];
    
   
    UIView *bgColorView = [[UIView alloc] init]; // 51 168 233
    bgColorView.backgroundColor = [UIColor colorWithRed:51.0 /255 green:168.0 / 255 blue:233.0 / 255 alpha:1];
    bgColorView.layer.masksToBounds = YES;
    cell.selectedBackgroundView = bgColorView;
    return cell;
}

-(void)modalPassWordVC
{
    _passVC.popoverPresentationController.delegate=self;
    _passVC.popoverPresentationController.sourceView=self.view;
    _passVC.popoverPresentationController.sourceRect=CGRectMake(self.view.center.x, self.view.center.y, 0, 0);
    _passVC.popoverPresentationController.permittedArrowDirections=NO;
}

-(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller
{
    return UIModalPresentationNone;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *str = GetSys_RightCode;
    if ([str containsString:@"P010201"]) {
//        _dataSource=@[@"值班登记",@"消息推送",@"意见反馈",@"离线地图",@"修改密码",@"退出"];
        switch (indexPath.row) {
            case 0:
                NSLog(@"0");
                _workReg.popoverPresentationController.delegate=self;
                _workReg.popoverPresentationController.sourceView=self.view;
                _workReg.popoverPresentationController.sourceRect=CGRectMake(self.view.center.x, self.view.center.y, 0, 0);
                _workReg.popoverPresentationController.permittedArrowDirections=NO;
                [self presentViewController:_workReg animated:YES completion:nil];
                break;
            case 1:
                [self.navigationController pushViewController:_pushVC animated:YES];
                break;
            case 2:
                [self.navigationController pushViewController:_FaceBookVc animated:YES];
                break;
            case 3:
                 [self.navigationController pushViewController:_detailViewController animated:YES];
                break;
            case 4:
                [self modalPassWordVC];
                [self presentViewController:_passVC animated:YES completion:nil];
                break;
            case 5:
                [self alertExit];
                break;
            default:
                break;
        }

    }else {
        switch (indexPath.row) {
            case 0:
                [self.navigationController pushViewController:_pushVC animated:YES];
                break;
            case 1:
                [self.navigationController pushViewController:_FaceBookVc animated:YES];
                break;
            case 2:
                [self.navigationController pushViewController:_detailViewController animated:YES];
                break;
            case 3:
                [self modalPassWordVC];
                [self presentViewController:_passVC animated:YES completion:nil];
                break;
            case 4:
                [self alertExit];
                break;
            default:
                break;
        }
    }
  }

//点击退出弹框提示
-(void)alertExit {
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"确定退出?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
    [alert show];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex==0) {
        return;
    }
    else {
     AppDelegate*delegate= (AppDelegate*)[UIApplication sharedApplication].delegate;
        [delegate.socket close];
        delegate.socket=nil;
        if (!([[[AppLocalManager sharedManager] getDataByKey:@"R"] integerValue] == 1)) {
            [[AppLocalManager sharedManager] DeleteDataByKey:@"RememberPassWord"];
        }
        LoginViewController *loginVc = [[LoginViewController alloc] init];
        [self presentViewController:loginVc animated:YES completion:nil];
    }
}


@end
